<?php

class edit_faqs
{
	var $mess;

	function edit_faqs()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select fq_question from ". FAQS_TABLE . " Where fq_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". FAQS_TABLE . " Where fq_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['fq_question'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". FAQS_TABLE . " (fq_question, fq_submittedby, fq_answer, fq_answeredby, fq_visible, fq_useful, fq_catagory) values('" . 
							$_POST['question'] . "', " . $_POST['submittedby'] . ", '" . $_POST['answer'] . "', " . $_POST['answeredby'] . 
							"," . $_POST['visible'] . "," . $_POST['useful'] . "," . $_POST['catagory'] . ")" ;
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['question'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". FAQS_TABLE . " set fq_question = '" . $_POST['question'] .
													  "', fq_submittedby = " . $_POST['submittedby'] .
													  ", fq_answer = '" . $_POST['answer'] .
													  "', fq_answeredby = " . $_POST['answeredby'] .
													  ", fq_visible = " . $_POST['visible'] .
													  ", fq_useful = " . $_POST['useful'] .
													  ", fq_catagory = " . $_POST['catagory'] .
													  " WHERE fq_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['question'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". FAQS_TABLE . " Where fq_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$row = formatArrayOutput($row, true);
		
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			//get the record
			$row['fq_catagoryOptions'] = '';
			$qc = "Select fc_id, fc_name From ". FAQCATAGORIES_TABLE . " ORDER BY fc_name ASC";
			$result = $db->sql_query($qc);
			while($cat = $db->sql_fetchrow($result)) 
			{
				$cat = formatArrayOutput($cat, true);
				$row['fq_catagoryOptions'] .= '<option value="' . $cat['fc_id'] . '" ' . 
											($cat['fc_id'] == $row['fq_catagory'] ? 'SELECTED' : '') . 
											'>' . $cat['fc_name'] . '</option>';
			}
		
			$row['fq_visibleOptions'] = '<option value="0" '. ($row['fq_visible']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['fq_visible']==1 ? 'SELECTED' : '').'>Yes</option>';
							 
			$row['fq_submittedOptions'] = '<option value="0" ' . 
											(0 == $row['fq_submittedby'] ? 'SELECTED' : '') . 
											'></option>';
			$row['fq_answeredOptions'] = '<option value="0" ' . 
											(0 == $row['fq_answeredby'] ? 'SELECTED' : '') . 
											'></option>';
			
			$drivers = "Select u.user_id, pf_drivername as user_drivername
					    From ". USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . LCUSERS_TABLE . " lc 
					    Where u.user_id = d.user_id AND u.user_id = lc.u_id
						ORDER BY pf_drivername ASC";
			$result = $db->sql_query($drivers);
			while($driver = $db->sql_fetchrow($result)) 
			{
				$driver = formatArrayOutput($driver, true);
				$row['fq_submittedOptions'] .= '<option value="' . $driver['user_id'] . '" ' . 
											($driver['user_id'] == $row['fq_submittedby'] ? 'SELECTED' : '') . 
											'>' . $driver['user_drivername'] . '</option>';
				$row['fq_answeredOptions'] .= '<option value="' . $driver['user_id'] . '" ' . 
											($driver['user_id'] == $row['fq_answeredby'] ? 'SELECTED' : '') . 
											'>' . $driver['user_drivername'] . '</option>';
			}
						
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
					
			include $skin_dir . 'admin/editfaq.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir, $db, $lang;
		
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		//get the record
		$row['fq_catagoryOptions'] = '';
		$qc = "Select fc_id, fc_name From ". FAQCATAGORIES_TABLE . " ORDER BY fc_name ASC";
		$result = $db->sql_query($qc);
		while($cat = $db->sql_fetchrow($result)) 
		{
			$row['fq_catagoryOptions'] .= '<option value="' . $cat['fc_id'] . '">' . $cat['fc_name'] . '</option>';
		}
			
		$row['fq_question'] = '';
		$row['fq_answer'] = '';
		$row['fq_visibleOptions'] = '<option value="0">No</option>' .
							 '<option value="1">Yes</option>';
		$row['fq_useful'] = '0';
	
		$row['fq_submittedOptions'] = '<option value="0"></option>';
		$row['fq_answeredOptions'] = '<option value="0"></option>';
		
		$drivers = "Select u.user_id, pf_drivername as user_drivername
					From ". USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . LCUSERS_TABLE . " lc 
					Where u.user_id = d.user_id AND u.user_id = lc.u_id
					ORDER BY pf_drivername ASC";
		$result = $db->sql_query($drivers);
		while($driver = $db->sql_fetchrow($result)) 
		{
			$row['fq_submittedOptions'] .= '<option value="' . $driver['user_id'] . '">' . $driver['user_drivername'] . '</option>';
			$row['fq_answeredOptions'] .= '<option value="' . $driver['user_id'] . '">' . $driver['user_drivername'] . '</option>';
		}
		
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editfaq.tpl';
	}
	
}
?>